/**
 * 2013，thor.com，all rights reserved。
 * 
 * Project：		thor-common
 * Filename：	QueryDefinition.java
 * Created：		2013-11-11
 * Creator：		thor
 */
package com.jeecg.query;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author thor
 * 
 */
public class QueryDefinition implements Serializable {
  private static final long serialVersionUID = -3351714623096181302L;

  public QueryDefinition() {
    // Do Nothing
  }

  public QueryDefinition(int pageNumber, int pageSize) {
    setPageNumber(pageNumber);
    setPageSize(pageSize);
  }

  private Collection<QueryCondition> conditions = new ArrayList<QueryCondition>();
  private List<QueryOrder> orders = new ArrayList<QueryOrder>();
  private int pageNumber = 0;
  private int pageSize = 0;

  public Collection<QueryCondition> getConditions() {
    return conditions;
  }

  public void setConditions(Collection<QueryCondition> conditions) {
    if (conditions == null) {
      this.conditions.clear();
    } else {
      this.conditions = conditions;
    }
  }

  public QueryCondition addCondition(String operation, Object... parameters) {
    QueryCondition condition = new QueryCondition(operation, parameters);
    this.conditions.add(condition);
    return condition;
  }

  public List<QueryOrder> getOrders() {
    return orders;
  }

  public void setOrders(List<QueryOrder> orders) {
    if (orders == null) {
      this.orders.clear();
    } else {
      this.orders = orders;
    }
  }

  public QueryOrder addOrder(String field) {
    QueryOrder order = new QueryOrder(field, null);
    this.orders.add(order);
    return order;
  }

  public QueryOrder addOrder(String field, QueryOrderDirection direction) {
    QueryOrder order = new QueryOrder(field, direction);
    this.orders.add(order);
    return order;
  }

  public int getPageNumber() {
    return pageNumber;
  }

  public void setPageNumber(int pageNumber) {
    this.pageNumber = pageNumber;
  }

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((conditions == null) ? 0 : conditions.hashCode());
    result = prime * result + ((orders == null) ? 0 : orders.hashCode());
    result = prime * result + pageNumber;
    result = prime * result + pageSize;
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    QueryDefinition other = (QueryDefinition) obj;
    if (conditions == null) {
      if (other.conditions != null)
        return false;
    } else if (!conditions.equals(other.conditions))
      return false;
    if (orders == null) {
      if (other.orders != null)
        return false;
    } else if (!orders.equals(other.orders))
      return false;
    if (pageNumber != other.pageNumber)
      return false;
    if (pageSize != other.pageSize)
      return false;
    return true;
  }
}
